<template>
  <div class="app-container" >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="论文题目" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入论文题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="论文类型" prop="paperType">
        <el-select v-model="queryParams.paperType" placeholder="请选择论文类型" clearable>
          <el-option
            v-for="dict in dict.type.paper_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="论文等级" prop="paperGrade">
        <el-input
          v-model="queryParams.paperGrade"
          placeholder="请输入论文等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="发表刊物/论文集" prop="publicationLournals" label-width="120px">
        <el-input
          v-model="queryParams.publicationLournals"
          placeholder="请输入发表刊物/论文集"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="学科门类" prop="subjectCategoryId">
        <el-select v-model="queryParams.subjectCategoryId" placeholder="请选择学科门类" clearable>
          <el-option
            v-for="dict in dict.type.subject_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="CN号" prop="cnNumber">
        <el-input
          v-model="queryParams.cnNumber"
          placeholder="请输入CN号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="期" prop="lssueVolume">
        <el-input
          v-model="queryParams.lssueVolume"
          placeholder="请输入期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="审核状态" prop="dataFacultyAuditStatus">
        <el-select v-model="queryParams.dataFacultyAuditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.faculty_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:paper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['work:paper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:paper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:paper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="!loading" v-loading="loading" :data="paperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="论文题目" align="center" prop="title" >
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)" style="color: #1c84c6;text-decoration:underline;cursor: pointer ">{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column label="作者名称" align="center" prop="authorName" />
      <el-table-column label="所属单位" align="center" prop="affiliatedUnit" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_unit" :value="scope.row.affiliatedUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="发表刊物/论文集" align="center" prop="publicationLournals" />
      <el-table-column label="审核状态" align="center" prop="dataFacultyAuditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.faculty_audit_status" :value="scope.row.dataFacultyAuditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['work:paper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:paper:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改论文对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
     <AddPaper :query-params="form" v-if="open" @handleClose="handleClose" :type="openView"></AddPaper>
    </el-dialog>
    <el-dialog  :visible.sync="openDetail" width="1200px" append-to-body>
      <paper-detail v-if="openDetail" :id="paperId" @close="openDetail= false"></paper-detail>
    </el-dialog>
  </div>
</template>

<script>
import { listPaper, getPaper, delPaper, addPaper, updatePaper } from "@/api/work/paper";
import { listWinners,delWinners } from "@/api/work/winners";
import paperDetail from "@/views/work/paper/Detail/index"
import AddPaper from "@/views/work/paper/AddPaper";
import {getUserProfile} from "@/api/system/user";


export default {
  name: "Paper",
  dicts: ['author_type', 'paper_type', 'work_unit', 'sys_yes_no', 'sys_user_sex', 'school_audit_status', 'subject_category', 'faculty_audit_status', 'education_backgorund', 'professional_title'],
  components:{AddPaper, paperDetail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWorkPaperWinners: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 论文表格数据
      paperList: [],
      // 论文完成人表格数据
      workPaperWinnersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      paperId:'',
      paperView:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        paperWinnersId: null,
        paperType: null,
        paperGrade: null,
        publicationTime: null,
        publicationLournals: null,
        lnclusionCategory: null,
        searching: null,
        subjectCategoryId: null,
        fistSubject: null,
        projectSource: null,
        dataSource: null,
        cnNumber: null,
        lssueVolume: null,
        agreeTime: null,
        filesUrl: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleDetail(row){
      this.paperId = row.id;
      this.openDetail=true;
    },
    /** 查询论文列表 */
    async getList() {
      this.loading = true;
      //获取当前登录用户id并添加筛选
      let userId = (await getUserProfile()).data.userId;
      this.queryParams.userId = userId;
      let response = await listPaper(this.queryParams)
        this.paperList = response.rows;

      let winnersList = (await  listWinners()).rows;
      for (const win of winnersList) {
        for (const paper of this.paperList) {
          if(win.paperId==paper.id&&win.signatureOrder=='1'){
            paper.authorName = win.authorName;
            paper.affiliatedUnit = win.affiliatedUnit;
          }
        }
      }
        this.total = response.total;
        this.loading = false;

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        paperWinnersId: null,
        paperType: null,
        paperGrade: null,
        correspondingAuthor: null,
        publicationTime: null,
        publicationLournals: null,
        lnclusionCategory: null,
        searching: null,
        subjectCategoryId: null,
        fistSubject: null,
        projectSource: null,
        dataSource: null,
        cnNumber: null,
        lssueVolume: null,
        agreeTime: null,
        filesUrl: null,
        remark: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId:null
      };
      this.workPaperWinnersList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.openView= true;
      this.reset();
      this.open = true;
      this.title = "添加论文";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.openView= false;
      this.reset();
      const id = row.id || this.ids
      getPaper(id).then(response => {
        this.form = response.data;
        this.workPaperWinnersList = response.data.workPaperWinnersList;
        this.open = true;
        this.title = "修改论文";
      });
    },
    /**addPaper关闭*/
    handleClose(){
      this.open = false;
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.workPaperWinnersList = this.workPaperWinnersList;
          if (this.form.id != null) {
            updatePaper(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaper(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除论文编号为"' + ids + '"的数据项？').then( function() {
        return delPaper(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 论文完成人序号 */
    rowWorkPaperWinnersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 论文完成人添加按钮操作 */
    handleAddWorkPaperWinners() {
      let obj = {};
      obj.signatureOrder = "";
      obj.authorType = "";
      obj.authorName = "";
      obj.gender = "";
      obj.educationBackgorund = "";
      obj.professionalTitle = "";
      obj.isCommunAuthor = "";
      obj.affiliatedUnit = "";
      obj.contributionRate = "";
      this.workPaperWinnersList.push(obj);
    },
    /** 论文完成人删除按钮操作 */
    handleDeleteWorkPaperWinners() {
      if (this.checkedWorkPaperWinners.length == 0) {
        this.$modal.msgError("请先选择要删除的论文完成人数据");
      } else {
        const workPaperWinnersList = this.workPaperWinnersList;
        const checkedWorkPaperWinners = this.checkedWorkPaperWinners;
        this.workPaperWinnersList = workPaperWinnersList.filter(function(item) {
          return checkedWorkPaperWinners.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWorkPaperWinnersSelectionChange(selection) {
      this.checkedWorkPaperWinners = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/paper/export', {
        ...this.queryParams
      }, `paper_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
